package com.leyou.auth.web;

import com.leyou.auth.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserAuthController {

    @Autowired
    private AuthUserService authUserService;

    /**
     * 用户登录生成cookie中存储token并返回cookie
     *
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void> userLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {

        this.authUserService.userLogin(username, password, response);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/logout")
    public ResponseEntity<Void> logout(
            HttpServletRequest request,
            HttpServletResponse response) {

        this.authUserService.logout(request, response);
        return ResponseEntity.ok().build();
    }
}
